import java.io.*;
import java.net.Socket;

public class TCPCoyFileClient {
    public static void main(String[] args) {
        try {
            Socket client = new Socket("127.0.0.1", 8888);
            System.out.println("客户端与服务器连接成功");

            String fileName  = "abc.txt";
            // 打开输出流
            OutputStream outputStream = client.getOutputStream();
            // 将文件名发送到服务器
            // outputStream.write(fileName.getBytes());
            outputStream.write((fileName+ "\r\n").getBytes());

            // 将文件的大小发送到服务器
            File file = new File(fileName);
            long size = file.length();
            outputStream.write((Long.toString(size)+"\r\n").getBytes());
            FileInputStream fileInputStream = new FileInputStream(fileName);

            byte[] bytes = new byte[8192];
            int length = 0;

            while (((length = fileInputStream.read(bytes)) != -1)) {
                outputStream.write(bytes, 0, length);
            }
            fileInputStream.close();

            client.shutdownOutput();

            byte[] bytes1 = new byte[8192];

            InputStream inputStream = client.getInputStream();
            length = inputStream.read(bytes1);

            String info = new String(bytes1, 0, length);
            System.out.println(info);

            outputStream.close();
            client.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
